package com.wss.lsl.mongo.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.net.UnknownHostException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.mongodb.gridfs.GridFsOperations;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.wss.lsl.mongo.demo.config.SpringMongoConfig;

/**
 * Hello world!
 * 
 */
public class App6 {

	public static void main(String[] args) throws UnknownHostException {

		ApplicationContext context = new AnnotationConfigApplicationContext(
				SpringMongoConfig.class);
		GridFsOperations operations = context.getBean("gridFsTemplate",
				GridFsOperations.class);

		DBObject metaData = new BasicDBObject();
		metaData.put("extra1", "anything 1");
		metaData.put("extra2", "anything 2");

		InputStream is = null;
		try {
			File file = new File("D:\\Backup\\我的文档\\My Pictures\\我休闲.jpg");
			is = new FileInputStream(file);
			operations.store(is, "我休闲.jpg",
					URLConnection.guessContentTypeFromName(file.getName()),
					metaData);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		System.out.println("Done");

	}
}
